package com.jsmy.marketing.activity;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.graphics.Paint;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jsmy.marketing.R;
import com.jsmy.marketing.adapter.ResourceMoreAdapter;
import com.jsmy.marketing.api.RetrofitManager;
import com.jsmy.marketing.base.BaseActivity;
import com.jsmy.marketing.bean.AppZLGroupBean;
import com.jsmy.marketing.bean.AppZLListBean;
import com.jsmy.marketing.common.Link;
import com.jsmy.marketing.utils.ShareUtil;
import com.jsmy.marketing.utils.ToastUtil;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.umeng.socialize.UMShareListener;
import com.umeng.socialize.bean.SHARE_MEDIA;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 更多资料
 */
public class ResouceMoreActivity extends BaseActivity implements UMShareListener {

    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_fenzu)
    TextView tvFenzu;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.m_rcycler)
    RecyclerView mRcycler;
    ResourceMoreAdapter adapter;
    List<AppZLListBean.DataBean.ListBean> list;
    int pageindex = 1;
    String groupcd = "";
    List<AppZLGroupBean.DataBean.ListBean> group;
    String[] items;

    String shareId;
    String sharePt;

    String fzmc = "";
    @Override
    protected int resId() {
        return R.layout.activity_resouce_more;
    }

    @Override
    protected void initData() {
        toolbar.setTitle("");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvFenzu.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG);
        adapter = new ResourceMoreAdapter();
        mRcycler.setLayoutManager(new LinearLayoutManager(this));
        mRcycler.setAdapter(adapter);

        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                pageindex = 1;
//                presenter.getappsyzlgdlist(groupcd, pageindex);
                requestData(groupcd);

            }
        });
        refreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                pageindex++;
                requestData(groupcd);
//                presenter.getappsyzlgdlist(groupcd, pageindex);
            }
        });

        adapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.img_tip:
                        shareId = list.get(position).getId();
                        ShareUtil.shareWeb(ResouceMoreActivity.this,((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getUrl(),
                                ((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getTitle(),
                                ((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getNote(),
                                ResouceMoreActivity.this);
                        break;
                    case R.id.linear_bar:
                        Bundle b=new Bundle();
                        b.putString("share","Y");
                        b.putString("id",((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getId());
                        b.putString("url",((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getUrl());
                        b.putString("title",((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getTitle());
                        b.putString("note",((AppZLListBean.DataBean.ListBean)adapter.getData().get(position)).getNote());
                        openActivity(ResouceDetailActivity.class,b);
                        break;
                }

            }
        });
        requestDataGroup();
    }

    private void requestDataGroup() {
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        mPresenter.loadData(RetrofitManager.getInstance().getappzlgrouplist(map), Link.APP_ZL_GROUP_LIST);
    }

    /**
     * 请求数据
     */
    private void requestData(String groupcd) {
        Map<String, Object> map2 = new HashMap<>();
        map2.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        if (!TextUtils.isEmpty(groupcd)){

            map2.put("groupcd", groupcd);
        }else {
            map2.put("groupcd", "");
        }
        map2.put("pageindex", "" + pageindex);
        mPresenter.loadData(RetrofitManager.getInstance().getappsyzlgdlist(map2), Link.APP_ZL_GD_LIST);
    }

    @Override
    public void refreshData(Object response, String position) {
        switch (position){


        case Link.APP_ZL_GD_LIST:
        if (!fzmc.isEmpty()){
            tvFenzu.setText(fzmc);
        }
        list = ((AppZLListBean) response).getData().getList();
        if (pageindex == 1) {
            refreshLayout.finishRefresh();
            adapter.replaceData(list);
        } else {
            refreshLayout.finishLoadmore();
            adapter.addData(list);
        }
        break;
        case Link.APP_ZL_GROUP_LIST:
        group = ((AppZLGroupBean) response).getData().getList();
        if (group.size() > 0) {
            groupcd = group.get(0).getId();
//            presenter.getappsyzlgdlist(groupcd, pageindex);
            requestData(groupcd);
            items = new String[group.size()];
            for (int i = 0; i < group.size(); i++) {
                items[i] = group.get(i).getMc();
            }
        }
        break;
        }
    }
    private void showListDialog() {
        AlertDialog.Builder listDialog =
                new AlertDialog.Builder(ResouceMoreActivity.this);
        listDialog.setTitle("选择分组");
        listDialog.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // which 下标从0开始
                groupcd = group.get(which).getId();
                fzmc = group.get(which).getMc();
//                presenter.getappsyzlgdlist(groupcd, pageindex);
                requestData(groupcd);
            }
        });
        listDialog.show();
    }

    @OnClick({R.id.tv_fenzu})
    public void onClick(View v){
        switch (v.getId()){
            case R.id.tv_fenzu:
                showListDialog();
                break;
        }
    }
    @Override
    public void onStart(SHARE_MEDIA share_media) {

    }

    @Override
    public void onResult(SHARE_MEDIA platform) {
        switch (platform) {
            case QQ:
                sharePt = "QQ";
                break;
            case QZONE:
                sharePt = "QZONE";
                break;
            case SINA:
                sharePt = "SINA";
                break;
            case WEIXIN:
                sharePt = "WEIXIN";
                break;
            case WEIXIN_CIRCLE:
                sharePt = "WEIXIN_CIRCLE";
                break;
            case SMS:
                sharePt = "SMS";
                break;
            case EMAIL:
                sharePt = "EMAIL";
                break;
        }
        Map<String, Object> map = new HashMap<>();
        map.put("lid", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("id", com.jsmy.marketing.manager.UserManager.getInstance().getId());
        map.put("planid", "");
        map.put("pt", sharePt);
        map.put("obj", "");
        mPresenter.loadData(RetrofitManager.getInstance().saveFxInfo(map), Link.APP_SAVE_FX_INFO);
    }

    @Override
    public void onError(SHARE_MEDIA share_media, Throwable throwable) {
        ToastUtil.showShort(this, "分享失败，"+throwable.getMessage());
    }

    @Override
    public void onCancel(SHARE_MEDIA share_media) {
        ToastUtil.showShort(this, "分享取消");
    }
}
